"""
订单模型（示例数据）
"""
from sqlalchemy import Column, String, Integer, ForeignKey, Text, Enum as SQLEnum, Numeric
from sqlalchemy.orm import relationship
import enum

from app.models.base import BaseModel


class OrderStatus(str, enum.Enum):
    """订单状态枚举"""
    PENDING = "pending"           # 待支付
    PAID = "paid"                 # 已支付
    PROCESSING = "processing"     # 处理中
    SHIPPED = "shipped"           # 已发货
    DELIVERED = "delivered"       # 已送达
    COMPLETED = "completed"       # 已完成
    CANCELLED = "cancelled"       # 已取消
    REFUNDING = "refunding"       # 退款中
    REFUNDED = "refunded"         # 已退款


class PaymentMethod(str, enum.Enum):
    """支付方式枚举"""
    ALIPAY = "alipay"         # 支付宝
    WECHAT = "wechat"         # 微信支付
    UNION_PAY = "union_pay"   # 银联
    COD = "cod"               # 货到付款


class ShippingMethod(str, enum.Enum):
    """配送方式枚举"""
    STANDARD = "standard"     # 标准配送
    EXPRESS = "express"       # 快递配送
    SELF_PICKUP = "self_pickup"  # 自提


class Order(BaseModel):
    """订单模型"""
    
    __tablename__ = "orders"
    
    # 关联用户
    user_id = Column(
        Integer,
        ForeignKey("users.id", ondelete="CASCADE"),
        nullable=False,
        index=True,
        comment="用户ID"
    )
    
    # 订单基本信息
    order_sn = Column(String(50), unique=True, index=True, nullable=False, comment="订单号")
    status = Column(
        SQLEnum(OrderStatus),
        default=OrderStatus.PENDING,
        nullable=False,
        index=True,
        comment="订单状态"
    )
    
    # 商品信息（简化存储，实际应该关联商品表）
    product_name = Column(String(200), nullable=False, comment="商品名称")
    product_sku = Column(String(100), nullable=True, comment="商品SKU")
    quantity = Column(Integer, default=1, comment="数量")
    
    # 价格信息
    original_price = Column(Numeric(10, 2), nullable=False, comment="原价")
    discount_amount = Column(Numeric(10, 2), default=0, comment="优惠金额")
    shipping_fee = Column(Numeric(10, 2), default=0, comment="运费")
    total_amount = Column(Numeric(10, 2), nullable=False, comment="总金额")
    paid_amount = Column(Numeric(10, 2), nullable=True, comment="实付金额")
    
    # 支付信息
    payment_method = Column(
        SQLEnum(PaymentMethod),
        nullable=True,
        comment="支付方式"
    )
    payment_no = Column(String(100), nullable=True, comment="支付流水号")
    paid_at = Column(String(50), nullable=True, comment="支付时间")
    
    # 收货信息
    receiver_name = Column(String(100), nullable=False, comment="收货人姓名")
    receiver_phone = Column(String(20), nullable=False, comment="收货人电话")
    receiver_province = Column(String(50), nullable=True, comment="省份")
    receiver_city = Column(String(50), nullable=True, comment="城市")
    receiver_district = Column(String(50), nullable=True, comment="区县")
    receiver_address = Column(String(500), nullable=False, comment="详细地址")
    receiver_zipcode = Column(String(10), nullable=True, comment="邮编")
    
    # 配送信息
    shipping_method = Column(
        SQLEnum(ShippingMethod),
        default=ShippingMethod.STANDARD,
        comment="配送方式"
    )
    shipping_company = Column(String(50), nullable=True, comment="物流公司")
    shipping_no = Column(String(100), nullable=True, comment="物流单号")
    shipped_at = Column(String(50), nullable=True, comment="发货时间")
    delivered_at = Column(String(50), nullable=True, comment="签收时间")
    
    # 售后信息
    refund_reason = Column(String(500), nullable=True, comment="退款原因")
    refund_amount = Column(Numeric(10, 2), nullable=True, comment="退款金额")
    refund_at = Column(String(50), nullable=True, comment="退款时间")
    
    # 备注信息
    buyer_message = Column(Text, nullable=True, comment="买家留言")
    seller_memo = Column(Text, nullable=True, comment="卖家备注")
    cancel_reason = Column(String(500), nullable=True, comment="取消原因")
    
    # 关系
    user = relationship("User", back_populates="orders")
    
    def __repr__(self):
        return f"<Order(id={self.id}, order_sn={self.order_sn}, status={self.status})>"
    
    @property
    def is_paid(self) -> bool:
        """是否已支付"""
        return self.status not in [OrderStatus.PENDING, OrderStatus.CANCELLED]
    
    @property
    def can_cancel(self) -> bool:
        """是否可以取消"""
        return self.status in [OrderStatus.PENDING, OrderStatus.PAID]
    
    @property
    def can_refund(self) -> bool:
        """是否可以退款"""
        return self.status in [
            OrderStatus.PAID,
            OrderStatus.PROCESSING,
            OrderStatus.SHIPPED,
            OrderStatus.DELIVERED
        ]
    
    @property
    def full_address(self) -> str:
        """完整地址"""
        parts = [
            self.receiver_province,
            self.receiver_city,
            self.receiver_district,
            self.receiver_address
        ]
        return "".join(p for p in parts if p)

